#include <SimpleDHT.h>
#include "ESP.h"
#include "DATA.h"

//温湿度传感器
#define DHTPIN 4
#define LED 13
SimpleDHT11 dht11(DHTPIN);

ESPINIT onenet = ESPINIT();
DT data = DT(0);
int oldtemp=0;
int oldhum=0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial) {
    ;
  }
  Serial.println("start");
  pinMode(LED,OUTPUT);
  digitalWrite(LED,LOW);
  onenet.init();
}

void loop() {
  // put your main code here, to run repeatedly:
 byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Read DHT11 failed, err="); 
    Serial.println(err);
    delay(1000);
    return;
  }
  int temp= (int)temperature;
  int hum = (int)humidity;
  if(temp != oldtemp || hum !=oldhum)
  {
    oldtemp = temp;
    oldhum = hum;
    Serial.print("温度："); Serial.print(temp);Serial.print(" ℃, "); 
    Serial.print("湿度："); Serial.print(hum);Serial.println(" %RH");
    int len = data.createJson("temp",temp,"hum",hum);
    Serial.println(data.jsonMsg);
    onenet.connect();
    onenet.send(len,data.jsonMsg);
   }
 Serial.println("#--------#");
  //len = data.createJson("hum",hum);
  //Serial.println(data.jsonMsg);
 // onenet.connect();
  //onenet.send(len,data.jsonMsg);
  delay(10000);
}
